package com.sprouting.ops.service;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sprouting.ops.dao.DromaraFileStorageDao;
import com.sprouting.ops.dao.OpsThirdPartySiteUserDao;
import com.sprouting.ops.dao.OpsThirdPartyUserDao;
import com.sprouting.ops.model.DromaraFileStorage;
import com.sprouting.ops.model.OpsThirdPartySiteUser;
import com.sprouting.ops.model.OpsThirdPartyUser;
import com.sprouting.ops.model.qo.OpsThirdPartyUserQo;
import com.sprouting.ops.model.vo.OpsThirdPartyUserVo;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 厂商人员(OpsThirdPartyUser)(OPS_THIRD_PARTY_USER) 表服务实现类
 *
 * @author ：lx（长沙麓谷）
 * @date 创建时间：2025-07-31 21:53:11
 */
@Service
@Slf4j
public class OpsThirdPartyUserService extends ServiceImpl<OpsThirdPartyUserDao, OpsThirdPartyUser> {

    @Resource
    private OpsThirdPartyUserDao opsThirdPartyUserDao;
    @Resource
    private DromaraFileStorageDao dromaraFileStorageDao;
    @Resource
    private OpsThirdPartySiteUserDao opsThirdPartySiteUserDao;

    /**
     * 分页查询 厂商人员
     * @param page 分页参数
     * @param query 查询参数
     * @return
     */
    public Page<OpsThirdPartyUserVo> getListPage(Page page, OpsThirdPartyUserQo query){
        return opsThirdPartyUserDao.getListPage(page, query);
    }

    /**
     * 删除厂商人员
     * @param partyUser 厂商人员
     * @param opsUploadFile 人员头像
     */
    @Transactional(rollbackFor = Exception.class)
    public void delOpsThirdPartyUser(OpsThirdPartyUser partyUser, DromaraFileStorage opsUploadFile) {
        opsThirdPartyUserDao.deleteById(partyUser);
        // 删除图标
        if (opsUploadFile != null){
            dromaraFileStorageDao.updateById(opsUploadFile);
        }

        // 厂商人员
        LambdaUpdateWrapper<OpsThirdPartySiteUser> wrapperThirdPartySiteUser = new LambdaUpdateWrapper<>();
        wrapperThirdPartySiteUser.eq(OpsThirdPartySiteUser::getThirdPartyUserId, partyUser.getId());
        opsThirdPartySiteUserDao.delete(wrapperThirdPartySiteUser);
    }

    /**
     * 更新厂商人员
     * @param bo 厂商人员
     * @param uploadFileArray 人员头像
     */
    @Transactional(rollbackFor = Exception.class)
    public void updOpsThirdPartyUser(OpsThirdPartyUser bo, DromaraFileStorage[] uploadFileArray) {
        opsThirdPartyUserDao.updateById(bo);

        // 头像
        if (uploadFileArray[0] != null){
            // 新的
            dromaraFileStorageDao.updateById(uploadFileArray[0]);
        }
        if (uploadFileArray[1] != null){
            // 旧的文件
            dromaraFileStorageDao.updateById(uploadFileArray[1]);
        }
    }

    /**
     * 保存厂商人员
     * @param bo 厂商人员
     * @param unifiedFileInfo 人员头像
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveOpsThirdPartyUser(OpsThirdPartyUser bo, DromaraFileStorage unifiedFileInfo) {
        opsThirdPartyUserDao.insert(bo);
        // 头像
        if (unifiedFileInfo != null){
            dromaraFileStorageDao.updateById(unifiedFileInfo);
        }
    }
}
